"""
大语言模型工厂
"""
from typing import Dict, Any
from enum import Enum
from langchain.llms import OpenAI, HuggingFaceHub
from langchain.chat_models import ChatOpenAI
from langchain.llms.base import BaseLLM


class ModelType(Enum):
    """模型类型枚举"""
    OPENAI = "openai"
    CHATGPT = "chatgpt"
    HUGGINGFACE = "huggingface"


class ModelFactory:
    """
    大语言模型工厂类
    """
    def __init__(self):
        self._models: Dict[str, BaseLLM] = {}
    
    def get_model(self, model_name: str, **kwargs) -> BaseLLM:
        """
        获取指定名称的模型实例
        
        Args:
            model_name: 模型名称 (openai, chatgpt, huggingface)
            **kwargs: 模型参数
        """
        if model_name in self._models:
            return self._models[model_name]
        
        model = self._create_model(model_name, **kwargs)
        self._models[model_name] = model
        return model
    
    def _create_model(self, model_name: str, **kwargs) -> BaseLLM:
        """
        创建模型实例
        """
        if model_name == ModelType.OPENAI.value:
            return OpenAI(**kwargs)
        elif model_name == ModelType.CHATGPT.value:
            return ChatOpenAI(**kwargs)
        elif model_name == ModelType.HUGGINGFACE.value:
            return HuggingFaceHub(**kwargs)
        else:
            raise ValueError(f"不支持的模型类型: {model_name}")